package com.lecyon.farm.service.impl;

import com.lecyon.farm.base.RestResponse;
import com.lecyon.farm.entity.InvalidImageInformation;
import com.lecyon.farm.service.ISyncMessageService;
import com.lecyon.farm.util.ResultUtil;
import com.lecyon.farm.vo.sync.SyncFailMessage;
import com.lecyon.farm.vo.sync.SyncMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Yao Zheng
 * @Date 2020/4/27 13:13
 */
@Service
public class SyncMessageServiceImpl implements ISyncMessageService {

    private MongoTemplate mongoTemplate;

    /**
     * 保存同步发送消息
     *
     * @param entity 同步消息
     * @return MongoEquipmentStatusDetail
     */
    @Override
    public RestResponse<SyncMessage> save(SyncMessage entity) {
        RestResponse<SyncMessage> result = new RestResponse<>();
        entity = mongoTemplate.save(entity);
        return ResultUtil.success(result, entity);
    }

    /**
     * 保存同步发送失败的消息
     *
     * @param entity 同步消息
     * @return MongoEquipmentStatusDetail
     */
    @Override
    public RestResponse<SyncFailMessage> saveFailMessage(SyncFailMessage entity) {
        RestResponse<SyncFailMessage> result = new RestResponse<>();
        entity = mongoTemplate.save(entity);
        return ResultUtil.success(result, entity);
    }

    /**
     * 保存url无效的图片信息
     *
     * @param entity 图片信息
     * @return MongoEquipmentStatusDetail
     */
    @Override
    public RestResponse<InvalidImageInformation> saveInvalidImageInformation(InvalidImageInformation entity) {
        RestResponse<SyncFailMessage> result = new RestResponse<>();
        entity = mongoTemplate.save(entity);
        return ResultUtil.success(result, entity);
    }

    /**
     * 查询所有同步发送消息
     *
     * @return 同步发送消息列表
     */
    @Override
    public RestResponse<List<SyncMessage>> findAll() {
        RestResponse<List<SyncMessage>> result = new RestResponse<>();
        List<SyncMessage> resultList = mongoTemplate.findAll(SyncMessage.class);
        return ResultUtil.success(result, resultList);
    }

    /**
     * ----------------------------- 分割线 以下是setter方法，用于替换field上的@Autowired -----------------------------
     */
    @Autowired
    @Lazy
    public void setMongoTemplate(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

}
